var date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1
month = month >= 10 ? month : `0${month}`
var days = date.getDate()
days = days >= 10 ? days : `0${days}`
var timestamp = new Date().getTime()

function formatDate(format, time) {
  if (!time) {
    return ''
  }
  var t = new Date(time)
  var tf = function(i) {
    return (i < 10 ? '0' : '') + i
  }
  return format.replace(/yyyy|mm|dd|HH|MM|SS/g, function(a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear())
      case 'mm':
        return tf(t.getMonth() + 1)
      case 'dd':
        return tf(t.getDate())
      case 'HH':
        return tf(t.getHours())
      case 'MM':
        return tf(t.getMinutes())
      case 'SS':
        return tf(t.getSeconds())
    }
  })
}

// 近n天
export function getDays(val) {
  var nDay = timestamp - 24 * 60 * 60 * 1000 * (val - 1) // 过去n天
  var start = formatDate('yyyy-mm-dd 00:00:00', nDay)
  var end = `${year}-${month}-${days} 23:59:59`
  return { start: start, end: end }
}

// 近n周
export function getWeeks(val) {
  var timestamp2 = timestamp - 24 * 60 * 60 * 1000 * 7 * val
  var now = new Date(timestamp2)
  var nowDayOfWeek = now.getDay()
  timestamp2 = timestamp - 24 * 60 * 60 * 1000 * 7 * val - 24 * 60 * 60 * 1000 * (nowDayOfWeek - 1)
  var start = formatDate('yyyy-mm-dd 00:00:00', timestamp2)

  var nowTaW = new Date()
  var end = formatDate('yyyy-mm-dd 23:59:59', nowTaW)
  return { start: start, end: end }
}

// 近n月
export function getMonths(val) {
  var timestamp1 = timestamp - 24 * 60 * 60 * 1000 * 30 * val
  var start = formatDate('yyyy-mm-01 00:00:00', timestamp1)
  var end = `${year}-${month}-${days} 23:59:59`
  return { start: start, end: end }
}

// 近季
export function getSeasons() {
  var start, end, value
  if (month === '01' || month === '02' || month === '03') {
    start = `${year - 1}-10-01 00:00:00`
    end = `${year}-03-31 23:59:59`
    value = `${year - 1}年4季度-${year}年1季度`
  }
  if (month === '04' || month === '05' || month === '06') {
    start = `${year}-01-01 00:00:00`
    end = `${year}-06-30 23:59:59`
    value = `${year}年1季度-${year}年2季度`
  }
  if (month === '07' || month === '08' || month === '09') {
    start = `${year}-04-01 00:00:00`
    end = `${year}-09-30 23:59:59`
    value = `${year}年2季度-${year}年3季度`
  }
  if (month === '10' || month === '11' || month === '12') {
    start = `${year}-07-01 00:00:00`
    end = `${year}-12-31 23:59:59`
    value = `${year}年3季度-${year}年4季度`
  }
  return { start: start, end: end, value: value }
}

// 近1年
export function getYears() {
  var start = `${year - 1}-01-01 00:00:00`
  var end = `${year}-${month}-${days} 23:59:59`
  var value = `${year - 1}年 - ${year}年`
  return { start: start, end: end, value: value }
}

